package space.xxhui.contentproviderdemo.contnetprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.Log;

import space.xxhui.contentproviderdemo.database.DbOpenHelper;

/**
 * Created by hui on 2016/10/18.
 */

public class BookProvider extends ContentProvider{
    private static final String TAG =  "BookProvider";

    public static final String AUTHORITY = "space.xxhui.contentproviderdemo.contnetprovider.BookProvider";

    public static final Uri BOOK_CONTENT_URI = Uri.parse("content://"+AUTHORITY+"/book");

    public static final Uri USER_CONTENT_URI = Uri.parse("content://"+AUTHORITY+"/user");

    public static final int BOOK_URI_CODE = 0 ;

    public static final int USER_URI_CODE = 1 ;

    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

    //将uri和uri_code关联在一起
    static {
        URI_MATCHER.addURI(AUTHORITY,"book",BOOK_URI_CODE);
        URI_MATCHER.addURI(AUTHORITY,"user",USER_URI_CODE);
    }
    private Context context;

    private SQLiteDatabase db ;

    @Override
    public boolean onCreate() {
        Log.d(TAG, "onCreate , current thread : "+ Thread.currentThread().getName());
        context = getContext();
        initProviderDate();
        return true;
    }

    private void initProviderDate() {
        db = new DbOpenHelper(context).getWritableDatabase();
        db.execSQL("delete from "+DbOpenHelper.BOOK_TABLE_NAME);
        db.execSQL("delete from "+DbOpenHelper.USER_TABLE_NAME);
        db.execSQL("insert into book values (3,'Android');");
        db.execSQL("insert into book values (4,'iOS');");
        db.execSQL("insert into book values (5,'Html5');");
        db.execSQL("insert into user values (1,'jake',1)");
        db.execSQL("insert into user values (2,'xxhui',0)");
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Log.d(TAG, "query , current thread : "+ Thread.currentThread().getName());
        String table = getTableName(uri);
        if (table == null){
            throw new IllegalArgumentException("Unsupported URI:"+ uri);
        }
        return db.query(table,projection,selection,selectionArgs,null,null,sortOrder ,null);

    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        Log.d(TAG, "getType: ");
        return null;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Log.d(TAG, "insert: ");
        String table = getTableName(uri);
        if (table == null){
            throw new IllegalArgumentException("Unsupported URI:"+ uri);
        }
        context.getContentResolver().notifyChange(uri,null);
        return uri;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        Log.d(TAG, "delete: ");
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        Log.d(TAG, "update: ");
        String table = getTableName(uri);
        if (table == null){
            throw new IllegalArgumentException("Unsupported URI:"+ uri);
        }
        int row = db.update(table,values,selection,selectionArgs);
        if (row > 0){
            context.getContentResolver().notifyChange(uri,null);
        }
        return row;
    }

    private String getTableName(Uri uri){
        String tableName = null ;
        switch (URI_MATCHER.match(uri)){
            case BOOK_URI_CODE:
                tableName = DbOpenHelper.BOOK_TABLE_NAME;
                break;
            case  USER_URI_CODE:
                tableName = DbOpenHelper.USER_TABLE_NAME;
                break;
            default:
                break;
        }
        return tableName;
    }
}
